<template>
  <page-header-wrapper>
    <page-tab-list>
      <div class="table-page-search-box">
        <a-form layout="inline">
          <a-row :gutter="24">
            <a-col :md="8" :sm="24">
              <a-form-item label="店铺名称">
                <a-input placeholder="请输入店铺名称" v-model="queryParam.store_name"></a-input>
              </a-form-item>
            </a-col>
            <a-col :md="8" :sm="24">
              <a-form-item label="用户昵称">
                <a-input placeholder="请输入用户昵称" v-model="queryParam.nickname"></a-input>
              </a-form-item>
            </a-col>
            <a-col :md="8" :sm="24">
              <a-form-item label="手机号码">
                <div class="select">
                  <a-input placeholder="请输入手机号码" v-model="queryParam.mobile"></a-input>
                  <a-button type="primary" @click="getList">查询</a-button>
                </div>
              </a-form-item>
            </a-col>
          </a-row>
        </a-form>
      </div>
    </page-tab-list>
    <a-card :bordered="false" class="card_style_re">
      <div class="table-operator" style="display: flex; justify-content: space-between">
        <a-radio-group v-model="queryParam.status" @change="getList">
        </a-radio-group>
        <a-button type="primary" @click="toSetting(-1)">默认设置</a-button>
      </div>
      <a-table
        :rowKey="
          (r, i) => {
            return r.id
          }
        "
        :pagination="{
          total: this.totalCount,
          showSizeChanger: false,
          onChange: this.tablePageChange,
        }"
        :columns="columns"
        :data-source="list"
        :scroll="{ x: 'max-content' }"
      >
        <div slot="name" slot-scope="text, record">
          <div class="name">
            <div class="pic">
              <img :src="record.logo" alt="" />
            </div>
            <div class="info">
              <span>{{ record.store_name }}</span>
              <!-- <span>{{ record.tel }}</span> -->
            </div>
          </div>
        </div>
        <div slot="status" slot-scope="scope, record">
          <div class="status">
            <span :class="scope == 0 ? 'disabled' : 'open'"></span>
            <span>{{ scope == 0 ? '未开启' : '已开启' }}<span v-if="record.use_default" style="color:#65c817"><br>系统默认值</span></span>
            
          </div>
        </div>
        <span slot="level_one" slot-scope="scope"> {{ scope.level_one }}% </span>
        <span slot="level_two" slot-scope="scope"> {{ scope.level_two }}% </span>
        <span slot="level_three" slot-scope="scope"> {{ scope.level_three }}% </span>
        <div slot="action" slot-scope="scope">
          <a-button type="link" size="small" @click="toSetting(scope)">设置</a-button>
          <a-divider type="vertical" style="margin: 0 1px" />
          <a-dropdown>
            <a-button type="link" size="small"> 更多 <a-icon type="down" /></a-button>
            <a-menu slot="overlay">
              <a-menu-item>
                <a-popconfirm title="确认关闭吗？" :disabled="scope.switch == 0" @confirm="changeStatus(scope, 0)">
                  <a-button type="link" :disabled="scope.switch == 0">关闭</a-button>
                </a-popconfirm>
              </a-menu-item>
              <a-menu-item>
                <a-popconfirm title="确认开启吗？" :disabled="scope.switch != 0" @confirm="changeStatus(scope, 1)">
                  <a-button type="link" :disabled="scope.switch != 0">开启</a-button>
                </a-popconfirm>
              </a-menu-item>
            </a-menu>
          </a-dropdown>
        </div>
      </a-table>
    </a-card>
    <!-- 设置弹窗 -->
    <a-modal
      title="联盟分销设置"
      width="800px"
      v-model="showSetting"
      @ok="submitForm"
      :confirmLoading="btnLoading"
      :destroyOnClose="true"
    >
      <a-form-model
        ref="shareForm"
        :rules="rules"
        :model="shareInfo"
        :labelCol="{ span: 4 }"
        :wrapperCol="{ span: 18 }"
      >
        <a-form-model-item label="是否开启">
          <a-radio-group v-model="shareInfo.level">
            <a-radio :value="0"> 关闭 </a-radio>
            <a-radio :value="1" v-if="shareInfo.level == 0 || shareInfo.level == 1"> 开启 </a-radio>
            <a-radio :value="2" v-if="shareInfo.level == 2"> 开启 </a-radio>
            <a-radio :value="3" v-if="shareInfo.level == 3"> 开启 </a-radio>
          </a-radio-group>
        </a-form-model-item>
        <a-form-model-item label="分销级数" v-if="shareInfo.level != 0">
          <a-radio-group v-model="shareInfo.level">
            <a-radio :value="1"> 一级分销 </a-radio>
            <a-radio :value="2"> 二级分销 </a-radio>
            <a-radio :value="3"> 三级分销 </a-radio>
          </a-radio-group>
        </a-form-model-item>
        <a-form-model-item label="一级佣金" prop="level_one">
          <a-input type="number" min="0" addon-after="%" v-model="shareInfo.level_one" />
        </a-form-model-item>
        <a-form-model-item label="二级佣金" prop="level_two">
          <a-input type="number" min="0" addon-after="%" v-model="shareInfo.level_two" />
        </a-form-model-item>
        <a-form-model-item label="三级佣金" prop="level_three">
          <a-input type="number" min="0" addon-after="%" v-model="shareInfo.level_three" />
        </a-form-model-item>
      </a-form-model>
    </a-modal>
  </page-header-wrapper>
</template>

<script>
import { allianceDistributionList } from '@/api/saas/alliance'
import { setShareParams } from '@/api/store'
import tabList from '@/views/saas/components/tabList.vue'
import { cyyUploadImg } from '@/components/CyyUi'
const columns = [
  {
    title: 'ID',
    dataIndex: 'id',
  },
  {
    title: '店铺信息',
    scopedSlots: {
      customRender: 'name',
    },
    width: 250,
  },
  {
    title: '管理员',
    dataIndex: 'nickname',
  },
  {
    title: '手机号',
    dataIndex: 'mobile',
  },
  {
    title: '一级佣金',
    scopedSlots: {
      customRender: 'level_one',
    },
    sorter: (a, b) => a.level_one - b.level_one,
  },
  {
    title: '二级佣金',
    scopedSlots: {
      customRender: 'level_two',
    },
    sorter: (a, b) => a.level_two - b.level_two,
  },
  {
    title: '三级佣金',
    scopedSlots: {
      customRender: 'level_three',
    },
    sorter: (a, b) => a.level_three - b.level_three,
  },
  {
    title: '累计支出佣金',
    dataIndex: 'expenditure',
    sorter: (a, b) => a.expenditure - b.expenditure,
  },
  {
    title: '状态',
    dataIndex: 'switch',
    scopedSlots: {
      customRender: 'status',
    },
  },
  {
    title: '操作',
    scopedSlots: {
      customRender: 'action',
    },
  },
]

export default {
  components: {
    tabList,
    cyyUploadImg,
  },
  data() {
    return {
      // 搜索参数
      queryParam: {
        page: 1,
        mobile: '',
        store_name: '',
        nickname: '',
      },
      columns,
      list: [],
      // 是否显示设置分销弹窗
      showSetting: false,
      // 新增 编辑商户的表单
      shareInfo: {
        storeId: '',
        level: 0, // 是否开启分销
        level_one: 0, // 一级佣金
        level_two: 0, // 二级佣金
        level_three: 0, // 三级佣金
      },
      rules: {
        level_one: [{ required: true, message: '请输入一级佣金', trigger: 'blur' }],
        level_two: [{ required: true, message: '请输入二级佣金', trigger: 'blur' }],
        level_three: [{ required: true, message: '请输入三级佣金', trigger: 'blur' }],
      },
      btnLoading: false,
      defaultSet: [],
    }
  },
  created() {
    this.getList()
  },
  methods: {
    // 分页请求
    tablePageChange(e) {
      this.queryParam.page = e
      this.getList()
    },
    // 获取数据
    async getList() {
      const hide = this.$message.loading('加载中...', 1000)
      const res = await allianceDistributionList(this.queryParam)
      if (res.code == 0) {
        this.list = res.data.data
        this.totalCount = res.data.totalCount
        this.defaultSet = res.data.defaultSet
      }
      hide()
    },
    // 点击设置联盟分销
    toSetting(scope) {
      if(scope === -1){
        this.shareInfo.storeId = -1
        this.shareInfo.price_type = 0
        this.shareInfo.level_one = this.defaultSet.level_one - 0
        this.shareInfo.level = this.defaultSet.level - 0
        this.shareInfo.level_two = this.defaultSet.level_two - 0
        this.shareInfo.level_three = this.defaultSet.level_three - 0
        this.showSetting = true
        return;
      }
      this.shareInfo.storeId = scope.id
      this.shareInfo.price_type = 0
      this.shareInfo.level_one = scope.level_one - 0
      this.shareInfo.level = scope.switch - 0
      this.shareInfo.level_two = scope.level_two - 0
      this.shareInfo.level_three = scope.level_three - 0
      this.showSetting = true
    },
    // 点击确认设置
    editSetting() {
      this.$refs['settingForm'].validate(async (valid) => {
        if (valid) {
          this.btnLoading = true
          const res = await editShopSetting(this.settingForm)
          this.btnLoading = false
          if (res.code == 0) {
            this.showSetting = false
            this.getList()
          }
        } else {
          return false
        }
      })
    },
    submitForm() {
      this.$refs.shareForm.validate(async (valid) => {
        if (valid) {
          this.btnLoading = true
          const res = await setShareParams(this.shareInfo)
          this.btnLoading = false
          if (res.code == 0) {
            this.$message.success('保存成功！')
            this.showSetting = false
            this.getList()
          }
        }
      })
    },
    // 修改开启关闭状态
    async changeStatus(scope, status) {
      let param = {
        level: status,
        storeId: scope.id,
      }
      const res = await setShareParams(param)
      if (res.code == 0) {
        this.$message.success('修改成功！')
        this.getList()
      }
    },
  },
}
</script>
<style lang="less" scoped>
.tip {
  line-height: normal;
  margin-bottom: 0;
  font-size: 14px;
  color: rgba(0, 0, 0, 0.45);
}
.name {
  display: flex;
  align-items: center;
  .pic {
    width: 48px;
    min-width: 48px;
    height: 48px;
    border-radius: 4px;
    overflow: hidden;
    margin-right: 24px;
    img {
      width: 100%;
      height: 100%;
    }
  }
  .info {
    flex: auto;
    width: 0;
    display: flex;
    flex-direction: column;
    font-size: 14px;
    span {
      width: 100%;
      overflow: hidden;
      white-space: nowrap;
      text-overflow: ellipsis;
    }
    span:nth-child(2) {
      color: rgba(0, 0, 0, 0.45);
      margin-top: 6px;
    }
  }
}
.pic {
  width: 48px;
  min-width: 48px;
  height: 48px;
  border-radius: 4px;
}
.status {
  display: flex;
  align-items: center;
  span:nth-child(1) {
    width: 6px;
    height: 6px;
    border-radius: 50%;
    margin-right: 4px;
  }
  .close {
    background-color: #ff4400;
  }
  .disabled {
    background-color: #ccc;
  }
  .open {
    background-color: #65c817;
  }
}
</style>
